package com.work.controller;

import com.work.pojo.*;
import com.work.service.CartService;
import com.work.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;

    @RequestMapping("/getAllOrder")
    public ModelAndView getAllOrder(ModelAndView modelAndView) {
        List<Order> list = orderService.getAllOrder();
        modelAndView.addObject( "orderList", list );
        modelAndView.setViewName( "forward:/admin/showAllOrder.jsp" );
        return modelAndView;
    }

    @RequestMapping("/sendOrder")
    public ModelAndView sendOrder(String oid, ModelAndView modelAndView) {
        orderService.sendOrder( oid );
        List<Order> list = orderService.getAllOrder();
        modelAndView.addObject( "orderList", list );
        modelAndView.setViewName( "forward:/admin/showAllOrder.jsp" );
        return modelAndView;
    }

    @RequestMapping("/searchOrder")
    public ModelAndView searchOrder(ModelAndView modelAndView, String username, int stat) {
        List<Order> list = orderService.searchOrder( username, stat );
        modelAndView.addObject( "orderList", list );
        modelAndView.setViewName( "forward:/admin/showAllOrder.jsp" );
        return modelAndView;
    }

    @RequestMapping("/getOrderView")
    public ModelAndView getOrderView(HttpSession session, ModelAndView modelAndView) {
        User user = (User) session.getAttribute( "user" );
        Integer uid = user.getId();
        List<Cart> list = cartService.getCart( uid );
        List<Address> addresses = cartService.getAddress( uid );
        modelAndView.addObject( "carts", list );
        modelAndView.addObject( "addList", addresses );
        modelAndView.setViewName( "forward:/order.jsp" );
        return modelAndView;
    }

    @RequestMapping("/getOrderList")
    public ModelAndView getOrderList(HttpSession session, ModelAndView modelAndView) {
        User user = (User) session.getAttribute( "user" );
        Integer uid = user.getId();
        List<Order> list = orderService.getOrderById( uid );
        modelAndView.addObject( "orderList", list );
        modelAndView.setViewName( "forward:/orderList.jsp" );
        return modelAndView;
    }

    @RequestMapping("/addOrder")
    public ModelAndView addOrder(HttpSession session, ModelAndView modelAndView, String aid) {
        User user = (User) session.getAttribute( "user" );
        Integer uid = user.getId();
        int aidInt = Integer.parseInt( aid );
        Order order = orderService.addOrder( uid, aidInt );

        modelAndView.addObject( "order", order );
        modelAndView.setViewName( "forward:/orderSuccess.jsp" );
        return modelAndView;
    }

    @RequestMapping("/getOrderDetail")
    public ModelAndView getOrderDetail(String oid) {
        od od = orderService.getOrderDetail( oid );
        ModelAndView mv = new ModelAndView();
        mv.addObject( "od", od );
        mv.setViewName( "forward:/orderDetail.jsp" );
        return mv;
    }


    @RequestMapping("/changeStatus")
    public ModelAndView changeStatus(String oid) {
        orderService.changeStatus( oid );
        ModelAndView mv = new ModelAndView();
        mv.setViewName( "forward:/order/getOrderList" );
        return mv;
    }
}
